package top.fullj.remoting.message;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * De/Serialize with hessian(refer to http://hessian.caucho.com/)
 *
 * @author bruce.wu
 * @since 2022/2/14 9:58
 */
public class Message implements Serializable {

    private static final long serialVersionUID = 6992584488556118632L;

    private static final String HEADER_ID = "#ID";
    private static final String HEADER_TIMESTAMP = "#TS";

    private final Map<String, Object> headers = new HashMap<>();

    private Object payload;

    public Message() {
    }

    public Message(Object payload) {
        this.payload = payload;
    }

    public void setId(String id) {
        setHeader(HEADER_ID, id);
    }

    public String getId() {
        return getHeader(HEADER_ID);
    }

    public void setTimestamp(long ts) {
        setHeader(HEADER_TIMESTAMP, ts);
    }

    public long getTimestamp() {
        return getHeader(HEADER_TIMESTAMP);
    }

    public void setHeader(String key, Object value) {
        this.headers.put(key, value);
    }

    @SuppressWarnings("unchecked")
    public <T> T getHeader(String key) {
        return (T) headers.get(key);
    }

    public void setHeaders(Map<String, Object> headers) {
        this.headers.putAll(headers);
    }

    public Map<String, Object> getHeaders() {
        return Collections.unmodifiableMap(headers);
    }

    public Object getPayload() {
        return payload;
    }

}
